package com.deparse.dpframe.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * Created by MartinDKent on 2016/4/17.
 */
public class DPFileUtil {
    public static final String FILE_PREFIX = "file://";

    /**
     * Java文件操作 获取文件扩展名
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * Java文件操作 获取不带扩展名的文件名
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    public static String getExternalStorage() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static String getCacheDir(Context context) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        return cachePath;
    }

    public static String getCacheDir(Context context, String dirName) {
        dirName = getCacheDir(context) + File.separator + dirName;
        File file = new File(dirName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return dirName;
    }

    public static File makeCacheFile(Context context, String fileName) {
        return makeCacheFile(context, getCacheDir(context), fileName);
    }

    public static File makeCacheFile(Context context, String dirName, String fileName) {
        return makeFile(getCacheDir(context, dirName) + File.separator + fileName);
    }

    public static File makeDirs(String dirName) {
        File dir = new File(dirName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir;
    }

    public static File makeFile(String filePath) {
        File file = new File(filePath);
        makeDirs(file.getParent());
        if (file.exists()) {
            file.delete();
        }
        try {
            if (file.createNewFile()) {
                return file;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void emptyDir(File dir) {
        if (null != dir && dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    emptyDir(files[i]);
                }
                files[i].delete();
            }
        }
    }
}
